package com.chahu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chahu.entity.Comment;
import com.chahu.entity.SysUser;
import com.chahu.mapper.CommentMapper;
import com.chahu.service.CommentService;
import com.chahu.service.SysUserService;
import com.chahu.utils.UserThreadLocal;
import com.chahu.vo.CommentVo;
import com.chahu.vo.Result;
import com.chahu.vo.UserVo;
import com.chahu.vo.params.CommentParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public Result commentByArticleId(Long articleId) {
        LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLambdaQueryWrapper.eq(Comment::getArticleId, articleId);
        commentLambdaQueryWrapper.eq(Comment::getLevel, 1);
        List<Comment> comments = commentMapper.selectList(commentLambdaQueryWrapper);

        List<CommentVo> commentVos = copyList(comments);
        return Result.success(commentVos);
    }

    @Override
    public Result comment(CommentParam commentParam) {
        SysUser sysUser = UserThreadLocal.get();
        Comment comment = new Comment();
        comment.setArticleId(commentParam.getArticleId());
        comment.setAuthorId(sysUser.getId());
        comment.setContent(commentParam.getContent());
        comment.setCreateDate(System.currentTimeMillis());
        Long parent = commentParam.getParent();
        if(parent == null || parent == 0){
            comment.setLevel(1);
        }else {
            comment.setLevel(2);
        }

        comment.setParentId(parent == null ? 0 : parent);
        Long toUserId = commentParam.getToUserId();
        comment.setToUid(toUserId == null ? 0 : toUserId);
        commentMapper.insert(comment);
        return Result.success(null);
    }

    private List<CommentVo> copyList(List<Comment> records){
        ArrayList<CommentVo> commentVos = new ArrayList<>();
        for(Comment comment : records){
            commentVos.add(copy(comment));
        }
        return commentVos;
    }

    private CommentVo copy(Comment comment){
        CommentVo commentVo = new CommentVo();
        BeanUtils.copyProperties(comment, commentVo);
        commentVo.setId(comment.getId());

        Long authorId = comment.getAuthorId();
        UserVo userVoById = sysUserService.findUserVoById(authorId);
        commentVo.setAuthor(userVoById);

        Integer level = comment.getLevel();
        if(1 == level){
            Long id = comment.getId();
            List<CommentVo> commentsByParentId = findCommentsByParentId(id);
            commentVo.setChildrens(commentsByParentId);
        }

        if(level > 1){
            Long toUid = comment.getToUid();
            UserVo toUser = sysUserService.findUserVoById(toUid);
            commentVo.setToUser(toUser);
        }
        return commentVo;
    }

    private List<CommentVo> findCommentsByParentId(Long id){
        LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLambdaQueryWrapper.eq(Comment::getParentId, id);
        commentLambdaQueryWrapper.eq(Comment::getLevel, 2);
        List<Comment> comments = commentMapper.selectList(commentLambdaQueryWrapper);
        return copyList(comments);
    }
}
